package com.cxb.servicemap.remote;

import com.cxb.internalcommon.constant.AmapConfigConstants;
import com.cxb.internalcommon.dto.ResponseResult;
import com.cxb.internalcommon.responese.TerminalResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.omg.CORBA.LongLongSeqHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

@Service
public class TerminalClient {

    @Value("${amap.key}")
    private String amapKey;
    @Value("${amap.sid}")
    private String amapSid;
    @Autowired
    private RestTemplate restTemplate;

    public ResponseResult<TerminalResponse> addTerminal(String name,String desc){
        StringBuilder urlBuild = new StringBuilder();
        urlBuild.append(AmapConfigConstants.TERMINAL_ADD);
        urlBuild.append("?");
        urlBuild.append("key="+amapKey);
        urlBuild.append("&");
        urlBuild.append("sid="+amapSid);
        urlBuild.append("&");
        urlBuild.append("name="+name);
        urlBuild.append("&");
        urlBuild.append("desc="+desc);


        ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(urlBuild.toString(), null, String.class);
        /**
         *         {
         *             "errcode": 10000,
         *             "errmsg": "OK",
     *                 "data": {
     *                     "name": "车辆2",
     *                     "tid": 974313004,
     *                     "sid": 1036849
     *                  }
         *         }
         */
        String Body = stringResponseEntity.getBody();
        //转换成Json格式
        JSONObject result = JSONObject.fromObject(Body);
        JSONObject data = result.getJSONObject("data");
        String tid = data.getString("tid");
        TerminalResponse terminalResponse = new TerminalResponse();
        terminalResponse.setTid(tid);
        return ResponseResult.success(terminalResponse);
    }


    /**
     * 搜索周边
     * @param center
     * @param radius
     * @return
     */
    public ResponseResult<List<TerminalResponse>> aroundSearch(String center,Integer radius){
        StringBuilder url = new StringBuilder();
        url.append(AmapConfigConstants.AROUND_SEARCH);
        url.append("?");
        url.append("key="+amapKey);
        url.append("&");
        url.append("sid="+amapSid);
        url.append("&");
        url.append("center="+center);
        url.append("&");
        url.append("radius="+radius);

        System.out.println("终端周边搜索请求："+url.toString());
        ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(url.toString(), null, String.class);
        System.out.println("终端周边搜索响应:"+stringResponseEntity.getBody());

        //解析结果
        String body = stringResponseEntity.getBody();
        JSONObject jsonObject = JSONObject.fromObject(body);
        JSONObject data = jsonObject.getJSONObject("data");
        JSONArray results = data.getJSONArray("results");

        List<TerminalResponse> terminalResponseList = new ArrayList<>();
        for(int i=0;i<results.size();i++){
            TerminalResponse terminalResponse = new TerminalResponse();

            JSONObject jsonObject1 = results.getJSONObject(i);
            //desc是carId
            //防止精度丢失
            String desc = jsonObject1.getString("desc");
            Long carId = Long.parseLong(desc);
            String tid = jsonObject1.getString("tid");

            JSONObject location = jsonObject1.getJSONObject("location");
            String longitude = location.getString("longitude");
            String latitude = location.getString("latitude");


            terminalResponse.setCarId(carId);
            terminalResponse.setTid(tid);
            terminalResponse.setLatitude(latitude);
            terminalResponse.setLongitude(longitude);
            terminalResponseList.add(terminalResponse);
        }

        return ResponseResult.success(terminalResponseList);
    }
}
